import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3
import QtQml 2.12


ScrollView {

    clip: true
    ColumnLayout
    {
        Rectangle
        {
            Layout.fillWidth: true
            width: parent.width
            height: 60
            color: "#00000000"
            Text {
                x:10
                verticalAlignment: Text.AlignBottom
                text: qsTr("推荐内容")
                font.family: window.m_FONT_FAMLIY
                font.pointSize: 25
                color: "#eeffffff"
            }
        }

        MusicBannerView
        {
            id:bannerView
            Layout.preferredHeight: (Window.width-200)*0.3
            Layout.preferredWidth: Window.width-200
            Layout.fillWidth: true
            Layout.fillHeight: true
        }

        Rectangle
        {
            Layout.fillWidth: true
            width: parent.width
            height: 60
            color: "#00000000"
            Text {
                x:10
                verticalAlignment: Text.AlignBottom
                text: qsTr("热门歌单")
                font.family: window.m_FONT_FAMLIY
                font.pointSize: 25
                color: "#eeffffff"
            }
        }

        MusicGridHotView
        {
            id:hotView
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.preferredHeight: (window.width-250)*0.2*4+30*4+20
            Layout.bottomMargin: 20
        }

        Rectangle
        {
            Layout.fillWidth: true
            width: parent.width
            height: 60
            color: "#00000000"
            Text {
                x:10
                verticalAlignment: Text.AlignBottom
                text: qsTr("新歌推荐")
                font.family: window.m_FONT_FAMLIY
                font.pointSize: 25
                color: "#eeffffff"
            }
        }

        MusicGridLatesView
        {
            id:latesView
            Layout.fillHeight: true
            Layout.fillWidth: true
            Layout.preferredHeight: (window.width-230)*0.1*10+20
            Layout.bottomMargin: 20
        }


    }

    Component.onCompleted:
    {
        getBannerList()
    }

    function getBannerList()
    {
        loading.open()
        function onReply(reply)
        {
            loading.close()
            http.onReplySignal.disconnect(onReply)

            try{
                if(reply.length<1)
                {
                    notification.openError("请求轮播图图片为空！")
                    return
                }
                var banners = JSON.parse(reply).banners
                bannerView.bannerList = banners
                getHotList()
            }
            catch(err)
            {
                notification.openError("请求轮播图图片出错！")
            }
        }

        http.onReplySignal.connect(onReply)
        http.connet("banner")
    }

    function getHotList()
    {
        loading.open()
        function onReply(reply)
        {
            loading.close()
            http.onReplySignal.disconnect(onReply)
            try{
                if(reply.length<1)
                {
                    notification.openError("请求热门推荐为空！")
                    return
                }
                var playlists = JSON.parse(reply).playlists
                hotView.list = playlists
                getLatesList()
            }
            catch(err)
            {
                notification.openError("请求热门推荐出错！")
            }
            loading.close()
        }

        http.onReplySignal.connect(onReply)
        http.connet("top/playlist/highquality?limit=20")
    }

    function getLatesList()
    {
        loading.open()
        function onReply(reply)
        {
            loading.close()
            http.onReplySignal.disconnect(onReply)
            try{
                if(reply.length<1)
                {
                    notification.openError("请求最新歌曲为空！")
                    return
                }
                var lateslists = JSON.parse(reply).data
                latesView.list = lateslists.slice(0,30)
            }
            catch(err)
            {
                notification.openError("请求最新歌曲出错！")
            }
        }

        http.onReplySignal.connect(onReply)
        http.connet("top/song")
    }
}
